package org.jeecg.modules.qy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.qy.entity.QyAccount;
import org.jeecg.modules.qy.entity.QyBusinessInfo;
import org.jeecg.modules.qy.entity.QyCommodity;
import org.jeecg.modules.qy.entity.QyStore;
import org.jeecg.modules.qy.mapper.QyStoreMapper;
import org.jeecg.modules.qy.service.IQyAccountService;
import org.jeecg.modules.qy.service.IQyCommodityService;
import org.jeecg.modules.qy.service.IQyStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 店铺信息
 * @Author: jeecg-boot
 * @Date:   2021-03-14
 * @Version: V1.0
 */
@Service
public class QyStoreServiceImpl extends ServiceImpl<QyStoreMapper, QyStore> implements IQyStoreService {

    @Autowired
    private IQyCommodityService commodityService;

    @Autowired
    private IQyAccountService accountService;

    public QyStore getByCommodityId(String commodityId){
        List<QyStore> list = list(new QueryWrapper<QyStore>().eq("commodity_id",commodityId));
        return CollectionUtils.isNotEmpty(list)?list.get(0):null;
    }

    public void saveOrUpdateStore(QyStore qyStore,String companyId){
        if(StringUtils.isEmpty(qyStore.getId())){

            save(qyStore);
            QyCommodity commodity = commodityService.getById(qyStore.getCommodityId());
            commodity.setCommodityStatus(1);
            commodityService.updateById(commodity);
            QyAccount account = accountService.getByCompanyId(companyId);
            if(commodity.getCommodityType()==1){
                account.setCommoditySurplusNum(account.getCommoditySurplusNum()-1);
            }else{
                account.setMuchCommoditySurplusNum(account.getMuchCommoditySurplusNum()-1);
            }
            accountService.updateAccount(account);
        }else{
            updateById(qyStore);
            QyCommodity commodity = commodityService.getById(qyStore.getCommodityId());
            if(commodity.getCommodityStatus()==0){
                commodity.setCommodityStatus(1);
                commodityService.updateById(commodity);
            }
        }

    }
}
